package com.ytjj.qmyx.data.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 梦网短信发送工具类
 */
@Slf4j
@RefreshScope
@Service
public class SmsMengWangService {

    @Value("${mengwang.userid}")
    private String userid;

    @Value("${mengwang.pwd}")
    private String pwd;

    @Value("${mengwang.url}")
    private String url;

    /**
     * 短信发送
     * @param mobile  接收手机号
     * @param content 短信内容
     * @param content 短信签名
     */
    public String sendSmsWithResult(String mobile, String content,String sign) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        List<NameValuePair> param = new ArrayList<>();
        String timestamp = new SimpleDateFormat("MMddHHmmss").format(new Date());
        //账号+固定字符串+密码+时间戳
        String md5Pwd = MD5Encode(userid + "00000000" + pwd + timestamp);
        param.add(new BasicNameValuePair("userid", "JG1435"));
        param.add(new BasicNameValuePair("pwd", md5Pwd));
        param.add(new BasicNameValuePair("content", content));
        param.add(new BasicNameValuePair("timestamp", timestamp));
        param.add(new BasicNameValuePair("mobile", mobile));
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建请求内容
            HttpEntity entity = new UrlEncodedFormEntity(param, "utf-8");
            httpPost.setEntity(entity);
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }


    private String MD5Encode(String sourceString) {
        String resultString = null;
        try {
            resultString = new String(sourceString);
            MessageDigest md = MessageDigest.getInstance("MD5");
            resultString = byte2hexString(md.digest(resultString.getBytes()));
        } catch (Exception localException) {
        }
        return resultString;
    }

    private final String byte2hexString(byte[] bytes) {
        StringBuffer bf = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            if ((bytes[i] & 0xFF) < 16) {
                bf.append("0");
            }
            bf.append(Long.toString(bytes[i] & 0xFF, 16));
        }
        return bf.toString();
    }

}

